﻿using CoreWCF;
using SD.IdentitySystem;
using SD.Infrastructure.DTOBase;
using SD.Infrastructure.Membership;
using SlamDunk.MES.AppService.Maps;
using SlamDunk.MES.Domain.Entities.FoundationContext;
using SlamDunk.MES.Domain.Entities.ResourceContext;
using SlamDunk.MES.Domain.IRepositories;
using SlamDunk.MES.Domain.Mediators;
using SlamDunk.MES.IAppService.DTOs.Inputs;
using SlamDunk.MES.IAppService.DTOs.Outputs.FoundationContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.ResourceContext;
using SlamDunk.MES.IAppService.Interfaces;
using SlamDunk.ValueObjects.Enums;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SlamDunk.MES.AppService.Implements
{
    /// <summary>
    /// 资源定义服务契约实现
    /// </summary>
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, IncludeExceptionDetailInFaults = true)]
    public class ResourceContract : IResourceContract
    {
        #region # 字段及构造器

        /// <summary>
        /// 仓储中介者
        /// </summary>
        private readonly RepositoryMediator _repMediator;

        /// <summary>
        /// 单元事务
        /// </summary>
        private readonly IUnitOfWorkMES _unitOfWork;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public ResourceContract(RepositoryMediator repMediator, IUnitOfWorkMES unitOfWork)
        {
            this._repMediator = repMediator;
            this._unitOfWork = unitOfWork;
        }

        #endregion


        //命令部分

        #region # 创建人员类 —— void CreatePersonClass(Guid? parentNodeId, string classNo...
        /// <summary>
        /// 创建人员类
        /// </summary>
        /// <param name="parentNodeId">上级节点Id</param>
        /// <param name="classNo">人员类编号</param>
        /// <param name="className">人员类名称</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="description">描述</param>
        public void CreatePersonClass(Guid? parentNodeId, string classNo, string className, Guid? facilityId, string description)
        {
            PersonClass parentNode = parentNodeId.HasValue
                ? this._unitOfWork.Resolve<PersonClass>(parentNodeId.Value)
                : null;
            PersonClass personClass = new PersonClass(classNo, className, facilityId, description, parentNode);

            this._unitOfWork.RegisterAdd(personClass);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 修改人员类 —— void UpdatePersonClass(Guid personClassId, string classNo...
        /// <summary>
        /// 修改人员类
        /// </summary>
        /// <param name="personClassId">人员类Id</param>
        /// <param name="classNo">人员类编号</param>
        /// <param name="className">人员类名称</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="description">描述</param>
        public void UpdatePersonClass(Guid personClassId, string classNo, string className, Guid? facilityId, string description)
        {
            PersonClass personClass = this._unitOfWork.Resolve<PersonClass>(personClassId);
            personClass.UpdateInfo(classNo, className, facilityId, description);

            this._unitOfWork.RegisterSave(personClass);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 删除人员类 —— void RemovePersonClass(Guid personClassId)
        /// <summary>
        /// 删除人员类
        /// </summary>
        /// <param name="personClassId">人员类Id</param>
        public void RemovePersonClass(Guid personClassId)
        {
            PersonClass personClass = this._unitOfWork.Resolve<PersonClass>(personClassId);
            IList<Guid> relatedPersonClassIds = personClass.GetDeepNodeIds();

            this._unitOfWork.RegisterRemoveRange<PersonClass>(relatedPersonClassIds);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 创建人员 —— void CreatePerson(string personNo, string personName...
        /// <summary>
        /// 创建人员
        /// </summary>
        /// <param name="personNo">人员编号</param>
        /// <param name="personName">人员姓名</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="description">描述</param>
        public void CreatePerson(string personNo, string personName, Guid? facilityId, string description)
        {
            Person person = new Person(personNo, personName, facilityId, description);

            this._unitOfWork.RegisterAdd(person);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 修改人员 —— void UpdatePerson(Guid personId, string personNo...
        /// <summary>
        /// 修改人员
        /// </summary>
        /// <param name="personId">人员Id</param>
        /// <param name="personNo">人员编号</param>
        /// <param name="personName">人员姓名</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="description">描述</param>
        public void UpdatePerson(Guid personId, string personNo, string personName, Guid? facilityId, string description)
        {
            Person person = this._unitOfWork.Resolve<Person>(personId);
            person.UpdateInfo(personNo, personName, facilityId, description);

            this._unitOfWork.RegisterSave(person);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 删除人员 —— void RemovePerson(Guid personId)
        /// <summary>
        /// 删除人员
        /// </summary>
        /// <param name="personId">人员Id</param>
        public void RemovePerson(Guid personId)
        {
            this._unitOfWork.RegisterRemove<Person>(personId);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 关联人员到人员类 —— void RelatePersonToClasses(Guid personId...
        /// <summary>
        /// 关联人员到人员类
        /// </summary>
        /// <param name="personId">人员Id</param>
        /// <param name="personClassIds">人员类Id集</param>
        public void RelatePersonToClasses(Guid personId, IEnumerable<Guid> personClassIds)
        {
            Person person = this._unitOfWork.Resolve<Person>(personId);
            person.RelateClasses(personClassIds);

            this._unitOfWork.RegisterSave(person);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 创建班组 —— void CreateTeam(string teamNo, string teamName...
        /// <summary>
        /// 创建班组
        /// </summary>
        /// <param name="teamNo">班组编号</param>
        /// <param name="teamName">班组名称</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="description">描述</param>
        /// <param name="teamMemberParams">班组成员参数模型集</param>
        public void CreateTeam(string teamNo, string teamName, Guid? facilityId, string description, IEnumerable<TeamMemberParam> teamMemberParams)
        {
            #region # 验证

            teamMemberParams = teamMemberParams?.ToArray() ?? Array.Empty<TeamMemberParam>();
            if (!teamMemberParams.Any())
            {
                throw new ArgumentNullException(nameof(teamMemberParams), "班组成员不可为空！");
            }

            #endregion

            IEnumerable<TeamMember> teamMembers =
                from teamMemberParam in teamMemberParams
                select new TeamMember(teamMemberParam.personId, teamMemberParam.sort, teamMemberParam.description);

            Team team = new Team(teamNo, teamName, facilityId, description);
            team.SetMembers(teamMembers);

            this._unitOfWork.RegisterAdd(team);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 修改班组 —— void UpdateTeam(Guid teamId, string teamNo...
        /// <summary>
        /// 修改班组
        /// </summary>
        /// <param name="teamId">班组Id</param>
        /// <param name="teamNo">班组编号</param>
        /// <param name="teamName">班组名称</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="description">描述</param>
        /// <param name="teamMemberParams">班组成员参数模型集</param>
        public void UpdateTeam(Guid teamId, string teamNo, string teamName, Guid? facilityId, string description, IEnumerable<TeamMemberParam> teamMemberParams)
        {
            #region # 验证

            teamMemberParams = teamMemberParams?.ToArray() ?? Array.Empty<TeamMemberParam>();
            if (!teamMemberParams.Any())
            {
                throw new ArgumentNullException(nameof(teamMemberParams), "班组成员不可为空！");
            }

            #endregion

            IEnumerable<TeamMember> teamMembers =
                from teamMemberParam in teamMemberParams
                select new TeamMember(teamMemberParam.personId, teamMemberParam.sort, teamMemberParam.description);

            Team team = this._unitOfWork.Resolve<Team>(teamId);
            team.UpdateInfo(teamNo, teamName, facilityId, description);
            team.SetMembers(teamMembers);

            this._unitOfWork.RegisterSave(team);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 删除班组 —— void RemoveTeam(Guid teamId)
        /// <summary>
        /// 删除班组
        /// </summary>
        /// <param name="teamId">班组Id</param>
        public void RemoveTeam(Guid teamId)
        {
            this._unitOfWork.RegisterRemove<Team>(teamId);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 创建设备类 —— void CreateEquipmentClass(Guid? parentNodeId, string classNo...
        /// <summary>
        /// 创建设备类
        /// </summary>
        /// <param name="parentNodeId">上级节点Id</param>
        /// <param name="classNo">设备类编号</param>
        /// <param name="className">设备类名称</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="description">描述</param>
        public void CreateEquipmentClass(Guid? parentNodeId, string classNo, string className, Guid? facilityId, string description)
        {
            EquipmentClass parentNode = parentNodeId.HasValue
                ? this._unitOfWork.Resolve<EquipmentClass>(parentNodeId.Value)
                : null;
            EquipmentClass equipmentClass = new EquipmentClass(classNo, className, facilityId, description, parentNode);

            this._unitOfWork.RegisterAdd(equipmentClass);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 修改设备类 —— void UpdateEquipmentClass(Guid equipmentClassId, string classNo...
        /// <summary>
        /// 修改设备类
        /// </summary>
        /// <param name="equipmentClassId">设备类Id</param>
        /// <param name="classNo">设备类编号</param>
        /// <param name="className">设备类名称</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="description">描述</param>
        public void UpdateEquipmentClass(Guid equipmentClassId, string classNo, string className, Guid? facilityId, string description)
        {
            EquipmentClass equipmentClass = this._unitOfWork.Resolve<EquipmentClass>(equipmentClassId);
            equipmentClass.UpdateInfo(classNo, className, facilityId, description);

            this._unitOfWork.RegisterSave(equipmentClass);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 删除设备类 —— void RemoveEquipmentClass(Guid equipmentClassId)
        /// <summary>
        /// 删除设备类
        /// </summary>
        /// <param name="equipmentClassId">设备类Id</param>
        public void RemoveEquipmentClass(Guid equipmentClassId)
        {
            EquipmentClass equipmentClass = this._unitOfWork.Resolve<EquipmentClass>(equipmentClassId);
            IList<Guid> releatedEquipmentClassIds = equipmentClass.GetDeepNodeIds();

            this._unitOfWork.RegisterRemoveRange<EquipmentClass>(releatedEquipmentClassIds);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 创建设备 —— void CreateEquipment(string equipmentNo, string equipmentName...
        /// <summary>
        /// 创建设备
        /// </summary>
        /// <param name="equipmentNo">设备编号</param>
        /// <param name="equipmentName">设备名称</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="description">描述</param>
        public void CreateEquipment(string equipmentNo, string equipmentName, Guid? facilityId, string description)
        {
            Equipment equipment = new Equipment(equipmentNo, equipmentName, facilityId, description);

            this._unitOfWork.RegisterAdd(equipment);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 修改设备 —— void UpdateEquipment(Guid equipmentId, string equipmentNo...
        /// <summary>
        /// 修改设备
        /// </summary>
        /// <param name="equipmentId">设备Id</param>
        /// <param name="equipmentNo">设备编号</param>
        /// <param name="equipmentName">设备名称</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="description">描述</param>
        public void UpdateEquipment(Guid equipmentId, string equipmentNo, string equipmentName, Guid? facilityId, string description)
        {
            Equipment equipment = this._unitOfWork.Resolve<Equipment>(equipmentId);
            equipment.UpdateInfo(equipmentNo, equipmentName, facilityId, description);

            this._unitOfWork.RegisterSave(equipment);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 删除设备 —— void RemoveEquipment(Guid equipmentId)
        /// <summary>
        /// 删除设备
        /// </summary>
        /// <param name="equipmentId">设备Id</param>
        public void RemoveEquipment(Guid equipmentId)
        {
            this._unitOfWork.RegisterRemove<Equipment>(equipmentId);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 关联设备到设备类 —— void RelateEquipmentToClasses(Guid equipmentId...
        /// <summary>
        /// 关联设备到设备类
        /// </summary>
        /// <param name="equipmentId">设备Id</param>
        /// <param name="equipmentClassIds">设备类Id集</param>
        public void RelateEquipmentToClasses(Guid equipmentId, IEnumerable<Guid> equipmentClassIds)
        {
            Equipment equipment = this._unitOfWork.Resolve<Equipment>(equipmentId);
            equipment.RelateClasses(equipmentClassIds);

            this._unitOfWork.RegisterSave(equipment);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 创建物料类 —— void CreateMaterialClass(Guid? parentNodeId, string classNo...
        /// <summary>
        /// 创建物料类
        /// </summary>
        /// <param name="parentNodeId">上级节点Id</param>
        /// <param name="classNo">物料类编号</param>
        /// <param name="className">物料类名称</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="materialSourceType">物料来源类型</param>
        /// <param name="description">描述</param>
        public void CreateMaterialClass(Guid? parentNodeId, string classNo, string className, Guid? facilityId, MaterialSourceType? materialSourceType, string description)
        {
            MaterialClass parentNode = parentNodeId.HasValue
                ? this._unitOfWork.Resolve<MaterialClass>(parentNodeId.Value)
                : null;
            MaterialClass materialClass = new MaterialClass(classNo, className, facilityId, materialSourceType, description, parentNode);

            this._unitOfWork.RegisterAdd(materialClass);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 修改物料类 —— void UpdateMaterialClass(Guid materialClassId, string classNo...
        /// <summary>
        /// 修改物料类
        /// </summary>
        /// <param name="materialClassId">物料类Id</param>
        /// <param name="classNo">物料类编号</param>
        /// <param name="className">物料类名称</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="materialSourceType">物料来源类型</param>
        /// <param name="description">描述</param>
        public void UpdateMaterialClass(Guid materialClassId, string classNo, string className, Guid? facilityId, MaterialSourceType? materialSourceType, string description)
        {
            MaterialClass materialClass = this._unitOfWork.Resolve<MaterialClass>(materialClassId);
            materialClass.UpdateInfo(classNo, className, facilityId, materialSourceType, description);

            this._unitOfWork.RegisterSave(materialClass);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 删除物料类 —— void RemoveMaterialClass(Guid materialClassId)
        /// <summary>
        /// 删除物料类
        /// </summary>
        /// <param name="materialClassId">物料类Id</param>
        public void RemoveMaterialClass(Guid materialClassId)
        {
            MaterialClass materialClass = this._unitOfWork.Resolve<MaterialClass>(materialClassId);
            IList<Guid> relatedMaterialClassIds = materialClass.GetDeepNodeIds();

            this._unitOfWork.RegisterRemoveRange<MaterialClass>(relatedMaterialClassIds);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 创建物料定义 —— Guid CreateMaterial(string materialNo, string materialName...
        /// <summary>
        /// 创建物料定义
        /// </summary>
        /// <param name="materialNo">物料定义编号</param>
        /// <param name="materialName">物料定义名称</param>
        /// <param name="erpNo">ERP编号</param>
        /// <param name="standardUnitNo">基准单位编号</param>
        /// <param name="specification">规格</param>
        /// <param name="guaranteePeriod">保质期</param>
        /// <param name="materialSourceType">物料来源类型</param>
        /// <param name="hasLot">是否按批量管理</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="dataSource">数据来源</param>
        /// <param name="description">描述</param>
        public Guid CreateMaterial(string materialNo, string materialName, string erpNo, string standardUnitNo, string specification, TimeSpan guaranteePeriod, MaterialSourceType? materialSourceType, bool hasLot, Guid? facilityId, string dataSource, string description)
        {
            Material material = new Material(materialNo, materialName, erpNo, standardUnitNo, specification, guaranteePeriod, materialSourceType, hasLot, facilityId, dataSource, description);

            this._unitOfWork.RegisterAdd(material);
            this._unitOfWork.Commit();

            return material.Id;
        }
        #endregion

        #region # 修改物料定义 —— void UpdateMaterial(Guid materialId, string materialNo...
        /// <summary>
        /// 修改物料定义
        /// </summary>
        /// <param name="materialId">物料定义Id</param>
        /// <param name="materialNo">物料定义编号</param>
        /// <param name="materialName">物料定义名称</param>
        /// <param name="erpNo">ERP编号</param>
        /// <param name="standardUnitNo">基准单位编号</param>
        /// <param name="specification">规格</param>
        /// <param name="guaranteePeriod">保质期</param>
        /// <param name="materialSourceType">物料来源类型</param>
        /// <param name="hasLot">是否按批量管理</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="dataSource">数据来源</param>
        /// <param name="description">描述</param>
        public void UpdateMaterial(Guid materialId, string materialNo, string materialName, string erpNo, string standardUnitNo, string specification, TimeSpan guaranteePeriod, MaterialSourceType? materialSourceType, bool hasLot, Guid? facilityId, string dataSource, string description)
        {
            Material material = this._unitOfWork.Resolve<Material>(materialId);
            material.UpdateInfo(materialNo, materialName, erpNo, standardUnitNo, specification, guaranteePeriod, materialSourceType, hasLot, facilityId, dataSource, description);

            this._unitOfWork.RegisterSave(material);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 删除物料定义 —— void RemoveMaterial(Guid materialId)
        /// <summary>
        /// 删除物料定义
        /// </summary>
        /// <param name="materialId">物料定义Id</param>
        public void RemoveMaterial(Guid materialId)
        {
            this._unitOfWork.RegisterRemove<Material>(materialId);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 设置物料包装 —— void SetMaterialPack(Guid materialId...
        /// <summary>
        /// 设置物料包装
        /// </summary>
        /// <param name="materialId">物料定义Id</param>
        /// <param name="materialPackParam">物料包装参数模型</param>
        public void SetMaterialPack(Guid materialId, MaterialPackParam materialPackParam)
        {
            Material material = this._unitOfWork.Resolve<Material>(materialId);
            MaterialPack materialPack = null;
            if (materialPackParam != null)
            {
                materialPack = new MaterialPack(materialPackParam.maxPack, materialPackParam.maxPackCoefficient, materialPackParam.minPack, materialPackParam.minPackCoefficient);
            }

            material.SetMaterialPack(materialPack);

            this._unitOfWork.RegisterSave(material);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 设置物料条码设置 —— void SetMaterialPack(Guid materialId...
        /// <summary>
        /// 设置物料条码设置
        /// </summary>
        /// <param name="materialId">物料定义Id</param>
        /// <param name="materialBarcodeSettingParam">物料条码设置参数模型</param>
        public void SetMaterialBarcodeSetting(Guid materialId, MaterialBarcodeSettingParam materialBarcodeSettingParam)
        {
            Material material = this._unitOfWork.Resolve<Material>(materialId);
            MaterialBarcodeSetting materialBarcodeSetting = null;
            if (materialBarcodeSettingParam != null)
            {
                materialBarcodeSetting = new MaterialBarcodeSetting(materialBarcodeSettingParam.needPrintBarcode, materialBarcodeSettingParam.barcodeMode, materialBarcodeSettingParam.barcodeUnit);
            }

            material.SetMaterialBarcodeSetting(materialBarcodeSetting);

            this._unitOfWork.RegisterSave(material);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 关联物料定义到物料类 —— void RelateMaterialToClasses(Guid materialId...
        /// <summary>
        /// 关联物料定义到物料类
        /// </summary>
        /// <param name="materialId">物料定义Id</param>
        /// <param name="materialClassIds">物料类Id集</param>
        public void RelateMaterialToClasses(Guid materialId, IEnumerable<Guid> materialClassIds)
        {
            Material material = this._unitOfWork.Resolve<Material>(materialId);
            material.RelateClasses(materialClassIds);

            this._unitOfWork.RegisterSave(material);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 启用物料定义 —— void EnableMaterial(Guid materialId)
        /// <summary>
        /// 启用物料定义
        /// </summary>
        /// <param name="materialId">物料定义Id</param>
        public void EnableMaterial(Guid materialId)
        {
            LoginInfo loginInfo = MembershipMediator.GetLoginInfo();

            Material material = this._unitOfWork.Resolve<Material>(materialId);
            material.Enable(loginInfo?.LoginId, loginInfo?.RealName);

            this._unitOfWork.RegisterSave(material);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 停用物料定义 —— void DisableMaterial(Guid materialId...
        /// <summary>
        /// 停用物料定义
        /// </summary>
        /// <param name="materialId">物料定义Id</param>
        /// <param name="reason">停用原因</param>
        public void DisableMaterial(Guid materialId, string reason)
        {
            LoginInfo loginInfo = MembershipMediator.GetLoginInfo();

            Material material = this._unitOfWork.Resolve<Material>(materialId);
            material.Disable(loginInfo?.LoginId, loginInfo?.RealName, reason);

            this._unitOfWork.RegisterSave(material);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 创建企业 —— void CreateEnterprise(string enterpriseNo, string enterpriseName...
        /// <summary>
        /// 创建企业
        /// </summary>
        /// <param name="enterpriseNo">企业编号</param>
        /// <param name="enterpriseName">企业名称</param>
        /// <param name="enterpriseType">企业类型</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="address">联系地址</param>
        /// <param name="phone">联系电话</param>
        /// <param name="contact">联系人</param>
        /// <param name="fax">传真</param>
        /// <param name="legalPerson">法人</param>
        /// <param name="licenseNo">营业执照编号</param>
        /// <param name="description">描述</param>
        public void CreateEnterprise(string enterpriseNo, string enterpriseName, EnterpriseType enterpriseType, Guid? facilityId, string address, string phone, string contact, string fax, string legalPerson, string licenseNo, string description)
        {
            Enterprise enterprise = new Enterprise(enterpriseNo, enterpriseName, enterpriseType, facilityId, address, phone, contact, fax, legalPerson, licenseNo, description);

            this._unitOfWork.RegisterAdd(enterprise);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 修改企业 —— void UpdateEnterprise(Guid enterpriseId, string enterpriseNo...
        /// <summary>
        /// 修改企业
        /// </summary>
        /// <param name="enterpriseId">企业Id</param>
        /// <param name="enterpriseNo">企业编号</param>
        /// <param name="enterpriseName">企业名称</param>
        /// <param name="enterpriseType">企业类型</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="address">联系地址</param>
        /// <param name="phone">联系电话</param>
        /// <param name="contact">联系人</param>
        /// <param name="fax">传真</param>
        /// <param name="legalPerson">法人</param>
        /// <param name="licenseNo">营业执照编号</param>
        /// <param name="description">描述</param>
        public void UpdateEnterprise(Guid enterpriseId, string enterpriseNo, string enterpriseName, EnterpriseType enterpriseType, Guid? facilityId, string address, string phone, string contact, string fax, string legalPerson, string licenseNo, string description)
        {
            Enterprise enterprise = this._unitOfWork.Resolve<Enterprise>(enterpriseId);
            enterprise.UpdateInfo(enterpriseNo, enterpriseName, enterpriseType, facilityId, address, phone, contact, fax, legalPerson, licenseNo, description);

            this._unitOfWork.RegisterSave(enterprise);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 删除企业 —— void RemoveEnterprise(Guid enterpriseId)
        /// <summary>
        /// 删除企业
        /// </summary>
        /// <param name="enterpriseId">企业Id</param>
        public void RemoveEnterprise(Guid enterpriseId)
        {
            this._unitOfWork.RegisterRemove<Enterprise>(enterpriseId);
            this._unitOfWork.Commit();
        }
        #endregion


        //查询部分

        #region # 获取人员类列表 —— IEnumerable<PersonClassInfo> GetPersonClasses(string keywords...
        /// <summary>
        /// 获取人员类列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="parentNodeId">上级节点Id</param>
        /// <returns>人员类列表</returns>
        public IEnumerable<PersonClassInfo> GetPersonClasses(string keywords, Guid? facilityId, Guid? parentNodeId)
        {
            Facility facility = facilityId.HasValue
                ? this._repMediator.FacilityRep.SingleFully(facilityId.Value)
                : null;
            IList<Guid> relatedFacilityIds = facility?.GetDeepNodeIds();

            ICollection<PersonClass> personClasses = this._repMediator.PersonClassRep.FindDeeply(keywords, parentNodeId, relatedFacilityIds);

            IEnumerable<Guid> facilityIds = personClasses.Where(x => x.FacilityId.HasValue).Select(x => x.FacilityId.Value);
            IDictionary<Guid, FacilityInfo> facilityInfos = this._repMediator.FacilityRep.Find(facilityIds).ToDictionary(x => x.Key, x => x.Value.ToDTO());

            IEnumerable<PersonClassInfo> personClassInfos = personClasses.Select(x => x.ToDTO(facilityInfos));

            return personClassInfos;
        }
        #endregion

        #region # 获取根级人员类列表 —— IEnumerable<PersonClassInfo> GetRootPersonClasses()
        /// <summary>
        /// 获取根级人员类列表
        /// </summary>
        /// <returns>人员类列表</returns>
        public IEnumerable<PersonClassInfo> GetRootPersonClasses()
        {
            ICollection<PersonClass> personClasses = this._repMediator.PersonClassRep.FindRoots();

            IEnumerable<Guid> facilityIds = personClasses.Where(x => x.FacilityId.HasValue).Select(x => x.FacilityId.Value);
            IDictionary<Guid, FacilityInfo> facilityInfos = this._repMediator.FacilityRep.Find(facilityIds).ToDictionary(x => x.Key, x => x.Value.ToDTO());

            IEnumerable<PersonClassInfo> personClassInfos = personClasses.Select(x => x.ToDTO(facilityInfos));

            return personClassInfos;
        }
        #endregion

        #region # 根据Id获取人员类字典 —— IDictionary<Guid, PersonClassInfo> GetPersonClassesById(...
        /// <summary>
        /// 根据Id获取人员类字典
        /// </summary>
        /// <param name="personClassIds">人员类Id集</param>
        /// <returns>人员类字典</returns>
        public IDictionary<Guid, PersonClassInfo> GetPersonClassesById(IEnumerable<Guid> personClassIds)
        {
            #region # 验证

            personClassIds = personClassIds?.Distinct().ToArray() ?? Array.Empty<Guid>();
            if (!personClassIds.Any())
            {
                return new Dictionary<Guid, PersonClassInfo>();
            }

            #endregion

            IDictionary<Guid, PersonClass> personClasses = this._repMediator.PersonClassRep.Find(personClassIds);
            IDictionary<Guid, PersonClassInfo> personClassInfos = personClasses.ToDictionary(x => x.Key, x => x.Value.ToDTO(null));

            return personClassInfos;
        }
        #endregion

        #region # 分页获取人员列表 —— PageModel<PersonInfo> GetPersonsByPage(string keywords...
        /// <summary>
        /// 分页获取人员列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="personClassId">人员类Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>人员列表</returns>
        public PageModel<PersonInfo> GetPersonsByPage(string keywords, Guid? facilityId, Guid? personClassId, int pageIndex, int pageSize)
        {
            Facility facility = facilityId.HasValue
                ? this._repMediator.FacilityRep.SingleFully(facilityId.Value)
                : null;
            IList<Guid> relatedFacilityIds = facility?.GetDeepNodeIds();
            PersonClass personClass = personClassId.HasValue
                ? this._repMediator.PersonClassRep.SingleFully(personClassId.Value)
                : null;
            IList<Guid> relatedPersonClassIds = personClass?.GetDeepNodeIds();

            ICollection<Person> persons = this._repMediator.PersonRep.FindByPage(keywords, relatedFacilityIds, relatedPersonClassIds, pageIndex, pageSize, out int rowCount, out int pageCount);

            IEnumerable<Guid> facilityIds = persons.Where(x => x.FacilityId.HasValue).Select(x => x.FacilityId.Value);
            IDictionary<Guid, FacilityInfo> facilityInfos = this._repMediator.FacilityRep.Find(facilityIds).ToDictionary(x => x.Key, x => x.Value.ToDTO());

            IEnumerable<PersonInfo> personInfos = persons.Select(x => x.ToDTO(facilityInfos));

            return new PageModel<PersonInfo>(personInfos, pageIndex, pageSize, pageCount, rowCount);
        }
        #endregion

        #region # 根据Id获取人员字典 —— {Guid, PersonInfo} GetPersonsById(IEnumerable<Guid> personIds)
        /// <summary>
        /// 根据Id获取人员字典
        /// </summary>
        /// <param name="personIds">人员Id集</param>
        /// <returns>人员字典</returns>
        public IDictionary<Guid, PersonInfo> GetPersonsById(IEnumerable<Guid> personIds)
        {
            IDictionary<Guid, Person> persons = this._repMediator.PersonRep.Find(personIds);

            IEnumerable<Guid> facilityIds = persons.Values.Where(x => x.FacilityId.HasValue).Select(x => x.FacilityId.Value);
            IDictionary<Guid, FacilityInfo> facilityInfos = this._repMediator.FacilityRep.Find(facilityIds).ToDictionary(x => x.Key, x => x.Value.ToDTO());

            IDictionary<Guid, PersonInfo> personInfos = persons.ToDictionary(x => x.Key, x => x.Value.ToDTO(facilityInfos));

            return personInfos;
        }
        #endregion

        #region # 分页获取班组列表 —— PageModel<TeamInfo> GetTeamsByPage(string keywords...
        /// <summary>
        /// 分页获取班组列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>班组列表</returns>
        public PageModel<TeamInfo> GetTeamsByPage(string keywords, Guid? facilityId, int pageIndex, int pageSize)
        {
            Facility facility = facilityId.HasValue
                ? this._repMediator.FacilityRep.SingleFully(facilityId.Value)
                : null;
            IList<Guid> relatedFacilityIds = facility?.GetDeepNodeIds();

            ICollection<Team> teams = this._repMediator.TeamRep.FindByPage(keywords, relatedFacilityIds, pageIndex, pageSize, out int rowCount, out int pageCount);

            IEnumerable<Guid> facilityIds = teams.Where(x => x.FacilityId.HasValue).Select(x => x.FacilityId.Value);
            IDictionary<Guid, FacilityInfo> facilityInfos = this._repMediator.FacilityRep.Find(facilityIds).ToDictionary(x => x.Key, x => x.Value.ToDTO());

            IEnumerable<TeamInfo> teamInfos = teams.Select(x => x.ToDTO(facilityInfos));

            return new PageModel<TeamInfo>(teamInfos, pageIndex, pageSize, pageCount, rowCount);
        }
        #endregion

        #region # 获取班组成员列表 —— IEnumerable<TeamMemberInfo> GetTeamMembers(Guid teamId)
        /// <summary>
        /// 获取班组成员列表
        /// </summary>
        /// <param name="teamId">班组Id</param>
        /// <returns>班组成员列表</returns>
        public IEnumerable<TeamMemberInfo> GetTeamMembers(Guid teamId)
        {
            ICollection<TeamMember> teamMembers = this._repMediator.TeamMemberRep.Find(teamId);

            IEnumerable<Guid> personIds = teamMembers.Select(x => x.PersonId);
            IDictionary<Guid, PersonInfo> personInfos = this._repMediator.PersonRep.Find(personIds).ToDictionary(x => x.Key, x => x.Value.ToDTO(null));

            IEnumerable<TeamMemberInfo> teamMemberInfos = teamMembers.Select(x => x.ToDTO(personInfos));

            return teamMemberInfos;
        }
        #endregion

        #region # 获取设备类列表 —— IEnumerable<EquipmentClassInfo> GetEquipmentClasses(string keywords...
        /// <summary>
        /// 获取设备类列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="parentNodeId">上级节点Id</param>
        /// <returns>设备类列表</returns>
        public IEnumerable<EquipmentClassInfo> GetEquipmentClasses(string keywords, Guid? facilityId, Guid? parentNodeId)
        {
            Facility facility = facilityId.HasValue
                ? this._repMediator.FacilityRep.SingleFully(facilityId.Value)
                : null;
            IList<Guid> relatedFacilityIds = facility?.GetDeepNodeIds();

            ICollection<EquipmentClass> equipmentClasses = this._repMediator.EquipmentClassRep.FindDeeply(keywords, parentNodeId, relatedFacilityIds);

            IEnumerable<Guid> facilityIds = equipmentClasses.Where(x => x.FacilityId.HasValue).Select(x => x.FacilityId.Value);
            IDictionary<Guid, FacilityInfo> facilityInfos = this._repMediator.FacilityRep.Find(facilityIds).ToDictionary(x => x.Key, x => x.Value.ToDTO());

            IEnumerable<EquipmentClassInfo> equipmentClassInfos = equipmentClasses.Select(x => x.ToDTO(facilityInfos));

            return equipmentClassInfos;
        }
        #endregion

        #region # 获取根级设备类列表 —— IEnumerable<EquipmentClassInfo> GetRootEquipmentClasses()
        /// <summary>
        /// 获取根级设备类列表
        /// </summary>
        /// <returns>设备类列表</returns>
        public IEnumerable<EquipmentClassInfo> GetRootEquipmentClasses()
        {
            ICollection<EquipmentClass> equipmentClasses = this._repMediator.EquipmentClassRep.FindRoots();

            IEnumerable<Guid> facilityIds = equipmentClasses.Where(x => x.FacilityId.HasValue).Select(x => x.FacilityId.Value);
            IDictionary<Guid, FacilityInfo> facilityInfos = this._repMediator.FacilityRep.Find(facilityIds).ToDictionary(x => x.Key, x => x.Value.ToDTO());

            IEnumerable<EquipmentClassInfo> equipmentClassInfos = equipmentClasses.Select(x => x.ToDTO(facilityInfos));

            return equipmentClassInfos;
        }
        #endregion

        #region # 根据Id获取设备类字典 —— IDictionary<Guid, EquipmentClassInfo> GetEquipmentClassesById(...
        /// <summary>
        /// 根据Id获取设备类字典
        /// </summary>
        /// <param name="equipmentClassIds">设备类Id集</param>
        /// <returns>设备类字典</returns>
        public IDictionary<Guid, EquipmentClassInfo> GetEquipmentClassesById(IEnumerable<Guid> equipmentClassIds)
        {
            #region # 验证

            equipmentClassIds = equipmentClassIds?.Distinct().ToArray() ?? Array.Empty<Guid>();
            if (!equipmentClassIds.Any())
            {
                return new Dictionary<Guid, EquipmentClassInfo>();
            }

            #endregion

            IDictionary<Guid, EquipmentClass> equipmentClasses = this._repMediator.EquipmentClassRep.Find(equipmentClassIds);
            IDictionary<Guid, EquipmentClassInfo> equipmentClassInfos = equipmentClasses.ToDictionary(x => x.Key, x => x.Value.ToDTO(null));

            return equipmentClassInfos;
        }
        #endregion

        #region # 分页获取设备列表 —— PageModel<EquipmentInfo> GetEquipmentsByPage(string keywords...
        /// <summary>
        /// 分页获取设备列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="equipmentClassId">设备类Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>设备列表</returns>
        public PageModel<EquipmentInfo> GetEquipmentsByPage(string keywords, Guid? facilityId, Guid? equipmentClassId, int pageIndex, int pageSize)
        {
            Facility facility = facilityId.HasValue
                ? this._repMediator.FacilityRep.SingleFully(facilityId.Value)
                : null;
            IList<Guid> relatedFacilityIds = facility?.GetDeepNodeIds();
            EquipmentClass equipmentClass = equipmentClassId.HasValue
                ? this._repMediator.EquipmentClassRep.SingleFully(equipmentClassId.Value)
                : null;
            IList<Guid> relatedEquipmentClassIds = equipmentClass?.GetDeepNodeIds();

            ICollection<Equipment> equipments = this._repMediator.EquipmentRep.FindByPage(keywords, relatedFacilityIds, relatedEquipmentClassIds, pageIndex, pageSize, out int rowCount, out int pageCount);

            IEnumerable<Guid> facilityIds = equipments.Where(x => x.FacilityId.HasValue).Select(x => x.FacilityId.Value);
            IDictionary<Guid, FacilityInfo> facilityInfos = this._repMediator.FacilityRep.Find(facilityIds).ToDictionary(x => x.Key, x => x.Value.ToDTO());

            IEnumerable<EquipmentInfo> equipmentInfos = equipments.Select(x => x.ToDTO(facilityInfos));

            return new PageModel<EquipmentInfo>(equipmentInfos, pageIndex, pageSize, pageCount, rowCount);
        }
        #endregion

        #region # 根据Id获取设备字典 —— {Guid, EquipmentInfo} GetEquipmentsById(IEnumerable<Guid> equipmentIds)
        /// <summary>
        /// 根据Id获取设备字典
        /// </summary>
        /// <param name="equipmentIds">设备Id集</param>
        /// <returns>设备字典</returns>
        public IDictionary<Guid, EquipmentInfo> GetEquipmentsById(IEnumerable<Guid> equipmentIds)
        {
            IDictionary<Guid, Equipment> equipments = this._repMediator.EquipmentRep.Find(equipmentIds);

            IEnumerable<Guid> facilityIds = equipments.Values.Where(x => x.FacilityId.HasValue).Select(x => x.FacilityId.Value);
            IDictionary<Guid, FacilityInfo> facilityInfos = this._repMediator.FacilityRep.Find(facilityIds).ToDictionary(x => x.Key, x => x.Value.ToDTO());

            IDictionary<Guid, EquipmentInfo> equipmentInfos = equipments.ToDictionary(x => x.Key, x => x.Value.ToDTO(facilityInfos));

            return equipmentInfos;
        }
        #endregion

        #region # 根据编号获取设备字典 —— {string, EquipmentInfo} GetEquipmentsByNo(IEnumerable<string> equipmentNos)
        /// <summary>
        /// 根据编号获取设备字典
        /// </summary>
        /// <param name="equipmentNos">设备编号集</param>
        /// <returns>设备字典</returns>
        public IDictionary<string, EquipmentInfo> GetEquipmentsByNo(IEnumerable<string> equipmentNos)
        {
            IDictionary<string, Equipment> equipments = this._repMediator.EquipmentRep.Find(equipmentNos);

            IEnumerable<Guid> facilityIds = equipments.Values.Where(x => x.FacilityId.HasValue).Select(x => x.FacilityId.Value);
            IDictionary<Guid, FacilityInfo> facilityInfos = this._repMediator.FacilityRep.Find(facilityIds).ToDictionary(x => x.Key, x => x.Value.ToDTO());

            IDictionary<string, EquipmentInfo> equipmentInfos = equipments.ToDictionary(x => x.Key, x => x.Value.ToDTO(facilityInfos));

            return equipmentInfos;
        }
        #endregion

        #region # 获取物料类列表 —— IEnumerable<MaterialClassInfo> GetMaterialClasses(string keywords...
        /// <summary>
        /// 获取物料类列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="materialSourceType">物料来源类型</param>
        /// <param name="parentNodeId">上级节点Id</param>
        /// <returns>物料类列表</returns>
        public IEnumerable<MaterialClassInfo> GetMaterialClasses(string keywords, Guid? facilityId, MaterialSourceType? materialSourceType, Guid? parentNodeId)
        {
            Facility facility = facilityId.HasValue
                ? this._repMediator.FacilityRep.SingleFully(facilityId.Value)
                : null;
            IList<Guid> relatedFacilityIds = facility?.GetDeepNodeIds();

            ICollection<MaterialClass> materialClasses = this._repMediator.MaterialClassRep.FindDeeply(keywords, materialSourceType, parentNodeId, relatedFacilityIds);

            IEnumerable<Guid> facilityIds = materialClasses.Where(x => x.FacilityId.HasValue).Select(x => x.FacilityId.Value);
            IDictionary<Guid, FacilityInfo> facilityInfos = this._repMediator.FacilityRep.Find(facilityIds).ToDictionary(x => x.Key, x => x.Value.ToDTO());

            IEnumerable<MaterialClassInfo> materialClassInfos = materialClasses.Select(x => x.ToDTO(facilityInfos));

            return materialClassInfos;
        }
        #endregion

        #region # 获取根级物料类列表 —— IEnumerable<MaterialClassInfo> GetRootMaterialClasses()
        /// <summary>
        /// 获取根级物料类列表
        /// </summary>
        /// <returns>物料类列表</returns>
        public IEnumerable<MaterialClassInfo> GetRootMaterialClasses()
        {
            ICollection<MaterialClass> materialClasses = this._repMediator.MaterialClassRep.FindRoots();

            IEnumerable<Guid> facilityIds = materialClasses.Where(x => x.FacilityId.HasValue).Select(x => x.FacilityId.Value);
            IDictionary<Guid, FacilityInfo> facilityInfos = this._repMediator.FacilityRep.Find(facilityIds).ToDictionary(x => x.Key, x => x.Value.ToDTO());

            IEnumerable<MaterialClassInfo> materialClassInfos = materialClasses.Select(x => x.ToDTO(facilityInfos));

            return materialClassInfos;
        }
        #endregion

        #region # 根据Id获取物料类字典 —— IDictionary<Guid, MaterialClassInfo> GetMaterialClassesById(...
        /// <summary>
        /// 根据Id获取物料类字典
        /// </summary>
        /// <param name="materialClassIds">物料类Id集</param>
        /// <returns>物料类字典</returns>
        public IDictionary<Guid, MaterialClassInfo> GetMaterialClassesById(IEnumerable<Guid> materialClassIds)
        {
            #region # 验证

            materialClassIds = materialClassIds?.Distinct().ToArray() ?? Array.Empty<Guid>();
            if (!materialClassIds.Any())
            {
                return new Dictionary<Guid, MaterialClassInfo>();
            }

            #endregion

            IDictionary<Guid, MaterialClass> materialClasses = this._repMediator.MaterialClassRep.Find(materialClassIds);
            IDictionary<Guid, MaterialClassInfo> materialClassInfos = materialClasses.ToDictionary(x => x.Key, x => x.Value.ToDTO(null));

            return materialClassInfos;
        }
        #endregion

        #region # 分页获取物料定义列表 —— PageModel<MaterialInfo> GetMaterialsByPage(string keywords...
        /// <summary>
        /// 分页获取物料定义列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="materialSourceType">物料来源类型</param> 
        /// <param name="hasLot">是否按批量管理</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="enabled">是否启用</param>
        /// <param name="materialClassId">物料类Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>物料定义列表</returns>
        public PageModel<MaterialInfo> GetMaterialsByPage(string keywords, MaterialSourceType? materialSourceType, bool? hasLot, Guid? facilityId, bool? enabled, Guid? materialClassId, int pageIndex, int pageSize)
        {
            Facility facility = facilityId.HasValue
                ? this._repMediator.FacilityRep.SingleFully(facilityId.Value)
                : null;
            IList<Guid> relatedFacilityIds = facility?.GetDeepNodeIds();
            MaterialClass materialClass = materialClassId.HasValue
                ? this._repMediator.MaterialClassRep.SingleFully(materialClassId.Value)
                : null;
            IList<Guid> relatedMaterialClassIds = materialClass?.GetDeepNodeIds();

            ICollection<Material> materials = this._repMediator.MaterialRep.FindByPage(keywords, materialSourceType, hasLot, relatedFacilityIds, enabled, relatedMaterialClassIds, pageIndex, pageSize, out int rowCount, out int pageCount);

            IEnumerable<Guid> facilityIds = materials.Where(x => x.FacilityId.HasValue).Select(x => x.FacilityId.Value);
            IDictionary<Guid, FacilityInfo> facilityInfos = this._repMediator.FacilityRep.Find(facilityIds).ToDictionary(x => x.Key, x => x.Value.ToDTO());

            IEnumerable<MaterialInfo> materialInfos = materials.Select(x => x.ToDTO(facilityInfos));

            return new PageModel<MaterialInfo>(materialInfos, pageIndex, pageSize, pageCount, rowCount);
        }
        #endregion

        #region # 根据Id获取物料定义字典 —— {Guid, MaterialInfo} GetMaterialsById(IEnumerable<Guid> materialIds)
        /// <summary>
        /// 根据Id获取物料定义字典
        /// </summary>
        /// <param name="materialIds">物料定义Id集</param>
        /// <returns>物料定义字典</returns>
        public IDictionary<Guid, MaterialInfo> GetMaterialsById(IEnumerable<Guid> materialIds)
        {
            IDictionary<Guid, Material> materials = this._repMediator.MaterialRep.Find(materialIds);

            IEnumerable<Guid> facilityIds = materials.Values.Where(x => x.FacilityId.HasValue).Select(x => x.FacilityId.Value);
            IDictionary<Guid, FacilityInfo> facilityInfos = this._repMediator.FacilityRep.Find(facilityIds).ToDictionary(x => x.Key, x => x.Value.ToDTO());

            IDictionary<Guid, MaterialInfo> materialInfos = materials.ToDictionary(x => x.Key, x => x.Value.ToDTO(facilityInfos));

            return materialInfos;
        }
        #endregion

        #region # 根据编号获取物料定义字典 —— {string, MaterialInfo} GetMaterialsByNo(IEnumerable<string> materialNos)
        /// <summary>
        /// 根据编号获取物料定义字典
        /// </summary>
        /// <param name="materialNos">物料定义编号集</param>
        /// <returns>物料定义字典</returns>
        public IDictionary<string, MaterialInfo> GetMaterialsByNo(IEnumerable<string> materialNos)
        {
            IDictionary<string, Material> materials = this._repMediator.MaterialRep.Find(materialNos);

            IEnumerable<Guid> facilityIds = materials.Values.Where(x => x.FacilityId.HasValue).Select(x => x.FacilityId.Value);
            IDictionary<Guid, FacilityInfo> facilityInfos = this._repMediator.FacilityRep.Find(facilityIds).ToDictionary(x => x.Key, x => x.Value.ToDTO());

            IDictionary<string, MaterialInfo> materialInfos = materials.ToDictionary(x => x.Key, x => x.Value.ToDTO(facilityInfos));

            return materialInfos;
        }
        #endregion

        #region # 根据编号获取物料定义 —— MaterialInfo GetMaterialByNo(string materialNo)
        /// <summary>
        /// 根据编号获取物料定义
        /// </summary>
        /// <param name="materialNo">物料定义编号</param>
        /// <returns>物料定义</returns>
        public MaterialInfo GetMaterialByNo(string materialNo)
        {
            Material material = this._repMediator.MaterialRep.Single(materialNo);

            IEnumerable<Guid> facilityIds = new[] { material }.Where(x => x.FacilityId.HasValue).Select(x => x.FacilityId.Value);
            IDictionary<Guid, FacilityInfo> facilityInfos = this._repMediator.FacilityRep.Find(facilityIds).ToDictionary(x => x.Key, x => x.Value.ToDTO());

            MaterialInfo materialInfo = material.ToDTO(facilityInfos);

            return materialInfo;
        }
        #endregion

        #region # 是否存在物料定义编号 —— bool ExistMaterialNo(Guid? materialId, string materialNo)
        /// <summary>
        /// 是否存在物料定义编号
        /// </summary>
        /// <param name="materialId">物料定义Id</param>
        /// <param name="materialNo">物料定义编号</param>
        /// <returns>是否存在</returns>
        public bool ExistMaterialNo(Guid? materialId, string materialNo)
        {
            return this._repMediator.MaterialRep.ExistsNo(materialId, materialNo);
        }
        #endregion

        #region # 分页获取企业列表 —— PageModel<EnterpriseInfo> GetEnterprisesByPage(string keywords...
        /// <summary>
        /// 分页获取企业列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="enterpriseType">企业类型</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>企业列表</returns>
        public PageModel<EnterpriseInfo> GetEnterprisesByPage(string keywords, EnterpriseType? enterpriseType, Guid? facilityId, int pageIndex, int pageSize)
        {
            Facility facility = facilityId.HasValue
                ? this._repMediator.FacilityRep.SingleFully(facilityId.Value)
                : null;
            IList<Guid> relatedFacilityIds = facility?.GetDeepNodeIds();

            ICollection<Enterprise> enterprises = this._repMediator.EnterpriseRep.FindByPage(keywords, enterpriseType, relatedFacilityIds, pageIndex, pageSize, out int rowCount, out int pageCount);

            IEnumerable<Guid> facilityIds = enterprises.Where(x => x.FacilityId.HasValue).Select(x => x.FacilityId.Value);
            IDictionary<Guid, FacilityInfo> facilityInfos = this._repMediator.FacilityRep.Find(facilityIds).ToDictionary(x => x.Key, x => x.Value.ToDTO());

            IEnumerable<EnterpriseInfo> enterpriseInfos = enterprises.Select(x => x.ToDTO(facilityInfos));

            return new PageModel<EnterpriseInfo>(enterpriseInfos, pageIndex, pageSize, pageCount, rowCount);
        }
        #endregion
    }
}
